package com.school.aftersalemanagement.global;

import android.content.Context;
import android.content.SharedPreferences;

import com.school.aftersalemanagement.entity.User;

/**
 * Created by winkyle on 2017/10/6.
 */

public class UserConfig {
    public volatile static UserConfig userConfig;
    private Context context;
    private String UserInfoFile = "UserInfoFile";
    private String LoginAccountFile = "LoginAccountFile";
    private String Cookie = "Cookie";
    private String LoginAccountList = "LoginAccountList";
    private String DeviceToken = "DeviceToken";
    private UserConfig(Context context) {
        this.context = context;
    }

    public static UserConfig getInstance(Context context){
        if(userConfig == null){
            synchronized (UserConfig.class){
                if(userConfig == null)
                    userConfig = new UserConfig(context);
            }
        }
        if(userConfig.getContext()!=context)
            userConfig.setContext(context);
        return userConfig;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setCookie(String cookie) {
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        memberPrefs.edit().putString(Cookie,cookie).apply();
    }

    public String getCookie() {
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        return memberPrefs.getString(Cookie, "");
    }

    public void setLoginAccountList(String loginAccountList){
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        memberPrefs.edit().putString(LoginAccountList, loginAccountList).apply();
    }

    public String getLoginAccountList(){
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        return memberPrefs.getString(LoginAccountList, null);
    }

    public void setDeviceToken(String deviceToken){
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        memberPrefs.edit().putString(DeviceToken, deviceToken).apply();
    }

    public String getDeviceToken(){
        SharedPreferences memberPrefs = context.getSharedPreferences(
                LoginAccountFile, Context.MODE_PRIVATE);
        return memberPrefs.getString(DeviceToken, null);
    }

    public void setUser(String userinfo) {
        SharedPreferences memberPrefs = context.getSharedPreferences(
                UserInfoFile, Context.MODE_PRIVATE);
        memberPrefs.edit().putString(getCookie(), userinfo).apply();
    }

    public String getUser() {
        SharedPreferences memberPrefs = context.getSharedPreferences(
                UserInfoFile, Context.MODE_PRIVATE);
        return memberPrefs.getString(getCookie(), null);
    }

    public void clearUser() {
        SharedPreferences memberPrefs = context.getSharedPreferences(
                UserInfoFile, Context.MODE_PRIVATE);
        memberPrefs.edit().remove(getCookie());
    }
}
